package com.czl.lunt.interceptor;

import com.czl.lunt.config.AppConfig;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取session对象
        HttpSession session = request.getSession(false);
        if (session != null && session.getAttribute(AppConfig.USER_SESSION) != null) {
            //校验通过
            return true;
        }
        //校验不通过, 跳转到登录界面
        response.sendRedirect("/sign-in.html");
        //返回一个终端流程
        return HandlerInterceptor.super.preHandle(request, response, handler);
    }
}
